﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;


namespace EsaProjectRest
{

    public class User
    {
        ServiceRest service1 = new ServiceRest();
        //records the data of the company
        public Boolean RegisterCompany(string Email, string Password, string Logo, string Name, string Description)
        {

            try
            {
                service1.OpenConnection();

                string sql = string.Format("INSERT INTO [User] values('{0}','{1}','{2}','{3}','{4}')", Email, Password, Logo, Name, Description);

                SqlCommand com = new SqlCommand(sql, service1.conn);

                com.ExecuteNonQuery();

                service1.CloseConnection();

                return true;
            }
            catch (Exception e)
            {

                System.Console.WriteLine(e.Message);
                return false;

            }
        }
        //function to perform the login
        public string LoginCompany(string email, string password)
        {
            try
            {
                DataSet vds = new DataSet();
                string vsql = string.Format("SELECT Id,Email,Password FROM [User] WHERE (Email = '" + email + "') AND (Password = '" + password + "') ");
                
                service1.OpenConnection();
               
                SqlCommand vCom = new SqlCommand(vsql, service1.conn);
                SqlDataAdapter vda = new SqlDataAdapter(vCom);
                
                vda.Fill(vds);
                //save id user
                string id = vds.Tables[0].Rows[0]["Id"].ToString();
                vda.Dispose();
                
                service1.CloseConnection();

                return id;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return "0";


            }
        }
        //takes the user table name and logo
        public string[] GetDataUser(string id)
        {
            try
            {
                DataSet vds = new DataSet();
                string[] val = new string[2];
                string vsql = string.Format("SELECT Logo,Name FROM [User] WHERE (Id = '" + id + "') ");
           
                SqlCommand vCom = new SqlCommand(vsql, service1.conn);
                SqlDataAdapter vda = new SqlDataAdapter(vCom);
           
                vda.Fill(vds);
            
                //save logo and name
                val[0] = vds.Tables[0].Rows[0]["Logo"].ToString();
                val[1] = vds.Tables[0].Rows[0]["Name"].ToString();
                vda.Dispose();
                
                service1.CloseConnection();
                return val;
            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                return null;
            }
            

        }
        //verification which social networks are active
        public string[] GetServiceOn(string id)
        {
            DataSet vds = new DataSet();
            string[] val = new string[6];
            string vsql = string.Format("SELECT Facebook,Google,Twitter,Pinterest,Linkedin,Foursquare FROM [Service] WHERE (Id = '" + id + "') ");
            
            SqlCommand vCom = new SqlCommand(vsql, service1.conn);
            SqlDataAdapter vda = new SqlDataAdapter(vCom);
            
            vda.Fill(vds);
            
            try
            {
                //save access token to social network
                val[0] = vds.Tables[0].Rows[0]["Facebook"].ToString();
                val[1] = vds.Tables[0].Rows[0]["Google"].ToString();
                val[2] = vds.Tables[0].Rows[0]["Twitter"].ToString();
                val[3] = vds.Tables[0].Rows[0]["Pinterest"].ToString();
                val[4] = vds.Tables[0].Rows[0]["Linkedin"].ToString();
                val[5] = vds.Tables[0].Rows[0]["Foursquare"].ToString();
                
                vda.Dispose();
                
                service1.CloseConnection();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);

            }
            
            return val;

        }
        //updates the avatar
        public Boolean UpdateLogo(string id, string img)
        {
            try
            {
                service1.OpenConnection();
                string sql = string.Format("UPDATE [User] SET Logo='" + img + "' WHERE (Id = '" + id + "') ");
                SqlCommand com = new SqlCommand(sql, service1.conn);
                com.ExecuteNonQuery();
                return true;
            }catch (Exception e)
            {
                System.Console.WriteLine(e.Message);
                return false;

            }
        }
        //takes the avatars of active social network
        public string[] GetAvatarUser(string id)
        {
            DataSet vds = new DataSet();
            string[] val = new string[6];
            string vsql = string.Format("SELECT * FROM [AvatarSN] WHERE (Id = '" + id + "') ");

            SqlCommand vCom = new SqlCommand(vsql, service1.conn);
            SqlDataAdapter vda = new SqlDataAdapter(vCom);

            vda.Fill(vds);
            try
            {
                //save avatar to social network
                val[0] = vds.Tables[0].Rows[0]["Facebook"].ToString();
                val[1] = vds.Tables[0].Rows[0]["Google"].ToString();
                val[2] = vds.Tables[0].Rows[0]["Twitter"].ToString();
                val[3] = vds.Tables[0].Rows[0]["Pinterest"].ToString();
                val[4] = vds.Tables[0].Rows[0]["Linkedin"].ToString();
                val[5] = vds.Tables[0].Rows[0]["Foursquare"].ToString();
                vda.Dispose();

                service1.CloseConnection();

            }
            catch (Exception ex)
            {
                System.Console.WriteLine(ex.Message);

            }
            return val;

        }

    }


}
